package com.tanhua.management.service;



import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.tanhua.dubbo.server.api.ManageVideoApi;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.pojo.Video;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.management.mapper.UserInfoMapper;
import com.tanhua.management.pojo.UserInfo;
import com.tanhua.management.vo.PageResult;
import com.tanhua.management.vo.VideoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * @author why
 */
@Service
public class ManageVideoService {



    @Reference(version = "1.0.0")
    private ManageVideoApi manageVideoApi;

    //评论表
    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;

    @Autowired
    private UserInfoMapper userInfoMapper;



    public PageResult queryVideoByUid(Integer page, Integer pageSize, String sortProp, String sortOrder, String uid) {
        /**
         * 根据传入的参数作为条件查询
         */
      PageResult pageResult=new PageResult();


        //查询用户上传的视频记录
        PageInfo<Video> pageInfo = manageVideoApi.queryVideoByUid(Integer.parseInt(uid), page, pageSize, sortProp, sortOrder);
        //分页查询出来的数据进行封装数据
        List<Video> records = pageInfo.getRecords();

        if (CollectionUtils.isEmpty(records)){
            return null;
        }

        /**
         * publishId  是 video的id
         * records 中 有 视频的编号  发布时间 视频链接 封面图片 总记录数 计算总页数
         * 查询评论表中数据  统计 举报 点赞 评论 转发
         * 查询用户表 将 发布人 发布人id
         */

        // 封装用户的数据
        QueryWrapper<UserInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",Long.valueOf(uid));
        UserInfo userInfo = this.userInfoMapper.selectOne(wrapper);
        // 直接遍历 当前视频的id 将 评论类型，1-点赞，2-评论，3-喜欢  4 举报 5转发 记录总数存入

        List<VideoVo> list=new ArrayList<>();
        for (Video record : records) {

            VideoVo videoVo= new VideoVo();
            videoVo.setUserId(record.getUserId());  //发布人id
            videoVo.setPicUrl(record.getPicUrl());   //封面url
            videoVo.setCreateDate(record.getCreated());  //发布时间
            videoVo.setVideoUrl(record.getVideoUrl());  //视频url
            videoVo.setId(record.getId().toHexString());  //id

            videoVo.setNickname(userInfo.getNickName()); //发布人昵称

            //以下应该从redis中取，目前未交接 从数据库取
            videoVo.setLikeCount(this.quanZiApi.queryCommentCount(videoVo.getId(),1).intValue());   //点赞数
            videoVo.setReportCount(this.quanZiApi.queryCommentCount(videoVo.getId(),4).intValue());   //举报数量
            videoVo.setCommentCount(this.quanZiApi.queryCommentCount(videoVo.getId(),2).intValue());   //评论数
            videoVo.setForwardingCount(this.quanZiApi.queryCommentCount(videoVo.getId(),5).intValue());  //转发数

            list.add(videoVo);
        }

        pageResult.setPage(page);
        pageResult.setPageSize(pageSize);
        Integer pages=(pageInfo.getTotal() / pageSize) +1;
        pageResult.setPage(pages);
        pageResult.setCounts(pageInfo.getTotal());
        pageResult.setItems(list);

        return pageResult;
    }
}
